import instance from "@/main";
import orderDict from "@/dict/order";
import { isSeller } from "@/util/common";
import { btnClose } from "./btnHandle/close";
import { btnTally } from "./btnHandle/tally";
import { btnRevoke } from "./btnHandle/revoke";
import { btnModifyPrice } from "./btnHandle/modifyPrice";
import { btnModifyGoods } from "./btnHandle/modifyGoods";
import { btnDeal } from "./btnHandle/deal";
import { btnContract } from "./btnHandle/contract";

// 操作按钮
const btns = [
  // 确定价
  {
    name: "确认",
    clickEvent: "acceptConfirm",
    flag: "acceptConfirm"
  },
  {
    name: "拒绝",
    clickEvent: "refuseConfirm",
    flag: "refuseConfirm"
  },
  {
    name: "取消订单",
    clickEvent: "cancelConfirm",
    flag: "cancelConfirm"
  },
  // 点价
  {
    name: "确认",
    clickEvent: "acceptPricing",
    flag: "acceptPricing"
  },
  {
    name: "拒绝",
    clickEvent: "refusePricing",
    flag: "refusePricing"
  },
  // 买家 待挂单 取消订单
  {
    name: "取消订单",
    clickEvent: "cancelPricing",
    flag: "cancelPricing"
  },
  // 卖家 已挂单未成交 取消订单
  {
    name: "取消订单",
    clickEvent: "sellerCancelPricing",
    flag: "sellerCancelPricing"
  },
  // 卖家 已挂单未成交 取消订单（套保）
  {
    name: "取消订单",
    clickEvent: "sellerCancelPricingHedge",
    flag: "sellerCancelPricingHedge"
  },
  ...btnDeal, // 成交
  ...btnModifyPrice, // 改价
  ...btnModifyGoods, // 修改商品
  ...btnRevoke, // 撤单
  ...btnTally, // 结单
  ...btnClose, // 关闭
  ...btnContract, // 合同
  {
    name: "订单备注",
    clickEvent: "buyerRemarkOrder",
    flag: "buyerRemarkOrder"
  },
  {
    name: "订单备注",
    clickEvent: "sellerRemarkOrder",
    flag: "sellerRemarkOrder"
  }
];

function btnJudge(flag, row) {
  let res = false;
  let routeName = instance.$route.name;
  // // 是否超管
  // let isSuperAdmin = function() {
  //   return instance.$isSuperAdmin(
  //     instance.$store.state.user.userData &&
  //       instance.$store.state.user.userData.userState
  //   );
  // };
  // 是否管理员以上
  let isAdmin = function() {
    return instance.$store.state.user.isAdmin;
  };
  // // 是否点价人
  // let isCreatedUser = function() {
  //   return (
  //     row.userId ===
  //     (instance.$store.state.user.userData &&
  //       instance.$store.state.user.userData.userId)
  //   );
  // };
  // // 是否报价发布人
  // let isIssueUser = function() {
  //   return true;
  // };
  // 卖家点价
  if (
    routeName === "sellerPricingOrder" ||
    routeName === "sellerPricingOrderDetail"
  ) {
    switch (flag) {
      // 卖家取消订单：已挂单、未成交
      case "sellerCancelPricing":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          row.orderDealType === orderDict.dealStatus.without.value &&
          !row.isHedging;
        break;
      // 卖家取消订单（套保）：已挂单、未成交
      case "sellerCancelPricingHedge":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          row.orderDealType === orderDict.dealStatus.without.value &&
          row.isHedging;
        break;
      // 卖家确认点价订单：发布人、管理员
      case "acceptPricing":
        res = row.orderStatus === orderDict.pricingStatus.wait.value;
        break;
      // 卖家拒绝点价订单：发布人、管理员
      case "refusePricing":
        res = row.orderStatus === orderDict.pricingStatus.wait.value;
        break;
      // 卖家全部成交（非套保）：发布人、管理员
      case "allDeal":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          (row.orderDealType === orderDict.dealStatus.without.value ||
            row.orderDealType === orderDict.dealStatus.part.value) &&
          !row.isHedging;
        break;
      // 卖家全部成交（套保）：管理员
      case "allDealHedge":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          (row.orderDealType === orderDict.dealStatus.without.value ||
            row.orderDealType === orderDict.dealStatus.part.value) &&
          row.isHedging &&
          isAdmin();
        break;
      // 卖家部分成交：发布人、管理员、非套保
      case "partDeal":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          (row.orderDealType === orderDict.dealStatus.without.value ||
            row.orderDealType === orderDict.dealStatus.part.value) &&
          !row.isHedging;
        break;
      // 卖家同意撤单：发布人、管理员
      case "acceptRevokeOrder":
        res = row.orderStatus === orderDict.pricingStatus.revoke.value;
        break;
      // 卖家拒绝撤单：发布人、管理员
      case "rejectRevokeOrder":
        res = row.orderStatus === orderDict.pricingStatus.revoke.value;
        break;
      // 卖家同意改价：发布人、管理员
      case "acceptModifyPrice":
        res =
          row.orderStatus === orderDict.pricingStatus.applyModifyPrice.value;
        break;
      // 卖家拒绝改价：发布人、管理员
      case "rejectModifyPrice":
        res =
          row.orderStatus === orderDict.pricingStatus.applyModifyPrice.value;
        break;
      // 卖家修改价格：发布人、管理员、未成交
      case "modifyPrice":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          row.orderDealType === orderDict.dealStatus.without.value &&
          !row.isHedging;
        break;
      // 卖家修改价格（套保）：发布人、管理员、未成交
      case "modifyPriceHedge":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          row.orderDealType === orderDict.dealStatus.without.value &&
          row.isHedging;
        break;
      // 卖家同意结单：发布人、管理员
      case "acceptTally":
        res = row.orderStatus === orderDict.pricingStatus.buyerTally.value;
        break;
      // 卖家拒绝结单：发布人、管理员
      case "rejectTally":
        res = row.orderStatus === orderDict.pricingStatus.buyerTally.value;
        break;
      // 卖家结单：发布人、管理员
      case "tallyOrder":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          row.orderDealType === orderDict.dealStatus.part.value &&
          !row.isHedging;
        break;
      // 卖家申请关闭点价订单：发布人、管理员
      case "sellerApplyPricingClose":
        res = row.orderStatus === orderDict.pricingStatus.waitContract.value;
        break;
      // 卖家同意关闭点价订单：管理员
      case "sellerAcceptPricingClose":
        res =
          row.orderStatus === orderDict.pricingStatus.buyerClose.value &&
          isAdmin();
        break;
      // 卖家拒绝关闭点价订单：管理员
      case "sellerRejectPricingClose":
        res =
          row.orderStatus === orderDict.pricingStatus.buyerClose.value &&
          isAdmin();
        break;
      default:
        break;
    }
  }
  // 卖家确定价
  if (
    routeName === "sellerConfirmOrder" ||
    routeName === "sellerConfirmOrderDetail"
  ) {
    switch (flag) {
      // 卖家确认确定价订单：发布人、管理员
      case "acceptConfirm":
        res = row.orderStatus === orderDict.confirmStatus.wait.value;
        break;
      // 卖家拒绝确定价订单：发布人、管理员
      case "refuseConfirm":
        res = row.orderStatus === orderDict.confirmStatus.wait.value;
        break;
      // 卖家申请关闭确定价订单：发布人、管理员
      case "sellerApplyConfirmClose":
        res = row.orderStatus === orderDict.confirmStatus.waitContract.value;
        break;
      // 卖家同意关闭确定价订单：管理员
      case "sellerAcceptConfirmClose":
        res =
          row.orderStatus === orderDict.confirmStatus.buyerClose.value &&
          isAdmin();
        break;
      // 卖家拒绝关闭确定价订单：管理员
      case "sellerRejectConfirmClose":
        res =
          row.orderStatus === orderDict.confirmStatus.buyerClose.value &&
          isAdmin();
        break;
      default:
        break;
    }
  }
  // 买家点价
  if (
    routeName === "buyerPricingOrder" ||
    routeName === "buyerPricingOrderDetail"
  ) {
    switch (flag) {
      // 买家取消点价订单：点价人、管理员、非套保
      case "cancelPricing":
        res =
          row.orderStatus === orderDict.pricingStatus.wait.value &&
          !row.isHedging;
        break;
      // 买家撤单：点价人、管理员
      case "revokeOrder":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          row.orderDealType === orderDict.dealStatus.without.value &&
          !row.isHedging;
        break;
      // 买家撤单（套保）：点价人、管理员
      case "revokeOrderHedge":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          row.orderDealType === orderDict.dealStatus.without.value &&
          row.isHedging;
        break;
      // 买家申请改价：点价人、管理员
      case "applyModifyPrice":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          row.orderDealType === orderDict.dealStatus.without.value &&
          !row.isHedging;
        break;
      // 买家申请改价（套保）：点价人、管理员
      case "applyModifyPriceHedge":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          row.orderDealType === orderDict.dealStatus.without.value &&
          row.isHedging;
        break;
      // 买家申请结单：点价人、管理员、部分成交
      case "applyTallyOrder":
        res =
          row.orderStatus === orderDict.pricingStatus.yet.value &&
          row.orderDealType === orderDict.dealStatus.part.value;
        break;
      // 买家申请关闭点价订单：点价人、管理员
      case "buyerApplyPricingClose":
        res = row.orderStatus === orderDict.pricingStatus.waitContract.value;
        break;
      // 买家同意关闭点价订单：管理员
      case "buyerAcceptPricingClose":
        res =
          row.orderStatus === orderDict.pricingStatus.sellerClose.value &&
          isAdmin();
        break;
      // 买家拒绝关闭点价订单：管理员
      case "buyerRejectPricingClose":
        res =
          row.orderStatus === orderDict.pricingStatus.sellerClose.value &&
          isAdmin();
        break;
      // 买家同意修改商品：点价人、管理员
      case "buyerAcceptPricingModifyGoods":
        res = row.orderStatus === orderDict.pricingStatus.modifyGoods.value;
        break;
      // 买家拒绝修改商品：点价人、管理员
      case "buyerRejectPricingModifyGoods":
        res = row.orderStatus === orderDict.pricingStatus.modifyGoods.value;
        break;
      // 买家同意修改升贴水：点价人、管理员
      case "buyerAcceptPricingModifyPremium":
        res = row.orderStatus === orderDict.pricingStatus.modifyPremium.value;
        break;
      // 买家拒绝修改升贴水：点价人、管理员
      case "buyerRejectPricingModifyPremium":
        res = row.orderStatus === orderDict.pricingStatus.modifyPremium.value;
        break;
      // 买家同意修改基价
      case "acceptSellerModifyBasePrice":
        res =
          row.orderStatus ===
          orderDict.pricingStatus.sellerModifyBasePrice.value;
        break;
      // 买家拒绝修改基价
      case "rejectSellerModifyBasePrice":
        res =
          row.orderStatus ===
          orderDict.pricingStatus.sellerModifyBasePrice.value;
        break;
      default:
        break;
    }
  }
  // 买家确定价
  if (
    routeName === "buyerConfirmOrder" ||
    routeName === "buyerConfirmOrderDetail"
  ) {
    switch (flag) {
      // 买家取消确定价订单：点价人、管理员
      case "cancelConfirm":
        res = row.orderStatus === orderDict.confirmStatus.wait.value;
        break;
      // 买家申请关闭确定价订单：点价人、管理员
      case "buyerApplyConfirmClose":
        res = row.orderStatus === orderDict.confirmStatus.waitContract.value;
        break;

      // 买家同意关闭确定价订单：管理员
      case "buyerAcceptConfirmClose":
        res =
          row.orderStatus === orderDict.confirmStatus.sellerClose.value &&
          isAdmin();
        break;

      // 买家拒绝关闭确定价订单：管理员
      case "buyerRejectConfirmClose":
        res =
          row.orderStatus === orderDict.confirmStatus.sellerClose.value &&
          isAdmin();
        break;
      // 买家同意修改商品：点价人、管理员
      case "buyerAcceptConfirmModifyGoods":
        res = row.orderStatus === orderDict.confirmStatus.modifyGoods.value;
        break;
      // 买家拒绝修改商品：点价人、管理员
      case "buyerRejectConfirmModifyGoods":
        res = row.orderStatus === orderDict.confirmStatus.modifyGoods.value;
        break;
      // 买家同意修改含税单价
      case "acceptSellerModifyFirmPrice":
        res =
          row.orderStatus ===
          orderDict.pricingStatus.sellerModifyFirmPrice.value;
        break;
      // 买家拒绝修改含税单价
      case "rejectSellerModifyFirmPrice":
        res =
          row.orderStatus ===
          orderDict.pricingStatus.sellerModifyFirmPrice.value;
        break;
      default:
        break;
    }
  }
  switch (flag) {
    // 买家备注订单
    case "buyerRemarkOrder":
      res = !isSeller();
      break;
    // 卖家备注订单
    case "sellerRemarkOrder":
      res = isSeller();
      break;
    // **************************** 合同 **************************** start
    // 买家，生成采购合同：待生成合同
    case "createPurchaseContract":
      res =
        !isSeller() &&
        row.orderStatus === orderDict.pricingStatus.waitContract.value;
      break;
    // 买家，上传双签合同：待生成合同
    case "uploadPurchaseDoubleSignContract":
      res =
        !isSeller() &&
        row.orderStatus === orderDict.pricingStatus.waitContract.value;
      break;
    // 卖家，生成销售合同：待生成合同
    case "createSalesContract":
      res =
        isSeller() &&
        row.orderStatus === orderDict.pricingStatus.waitContract.value;
      break;
    // 卖家，上传双签合同：待生成合同
    case "uploadSalesDoubleSignContract":
      res =
        isSeller() &&
        row.orderStatus === orderDict.pricingStatus.waitContract.value;
      break;
    // 卖家查看合同详情
    case "sellerViewContractDetail":
      res =
        row.orderStatus === orderDict.confirmStatus.createdContract.value &&
        isSeller();
      break;
    // 买家查看合同详情
    case "buyerViewContractDetail":
      res =
        row.orderStatus === orderDict.confirmStatus.createdContract.value &&
        !isSeller();
      break;
    // **************************** 合同 **************************** end
    default:
      break;
  }
  return res;
}

let setBtns = function(row) {
  let res = [];
  btns.forEach(item => {
    // 判断是否拥有对应权限、对应的订单状态、是否管理员、当前用户是否点价人、发布人
    if (instance.$verifyBtn(item.flag)) {
      if (btnJudge(item.flag, row)) {
        res.push(item);
      }
    }
  });
  return res;
};

export default setBtns;
